﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ServiceModel;

namespace CommunicationServiceApplication
{
    public class WindowsServiceController : INotifyPropertyChanged
    {
        #region Properties
        public static WindowsServiceController Instance
        {
            get
            {
                if (instance == null)
                    instance = new WindowsServiceController();

                return instance;
            }
        }
        private static WindowsServiceController instance;

        public DateTime LastAction
        {
            get;
            set;
        }
        public Boolean IsWindowsServiceActive
        {
            get
            {
                return isWindowsServiceActive;
            }
            set
            {
                isWindowsServiceActive = value;
                OnPropertyChanged("IsWindowsServiceActive");
            }
        }
        private Boolean isWindowsServiceActive = false;

        public Guid ApplicationId
        {
            get
            {
                return applicationId;
            }
            set
            {
                applicationId = value;
                OnPropertyChanged("ApplicationId");
            }
        }

        protected List<Guid> AttachedApplicationsIds
        {
            get;
            private set;
        }
        private Guid applicationId = Guid.NewGuid();
        #endregion

        #region Constructors
        private WindowsServiceController()
        {
            AttachedApplicationsIds = new List<Guid>();
        }
        #endregion

        #region Methods
        public void Security(Guid attachedApplicationId)
        {
            if (!AttachedApplicationsIds.Contains(attachedApplicationId))
                AttachedApplicationsIds.Add(attachedApplicationId);
        }
        public Guid[] GetAttachedApplicationIds()
        {
            //var itemsToRemove = new List<Guid>();
            //foreach (var appGuid in AttachedApplicationsIds)
            //{
            //    using (var desktopApp = new DesktopApplicationConnection(this, appGuid))
            //    {
            //        if (!desktopApp.Ping())
            //            itemsToRemove.Add(appGuid);
            //    }
            //}
            //if (itemsToRemove.Count > 0)
            //{
            //    //Loop through all application Guids and remove them from the active list
            //    foreach (var appGuid in itemsToRemove)
            //    {
            //        AttachedApplicationsIds.Remove(appGuid);
            //    }
            //}
            return AttachedApplicationsIds.ToArray();
        }
        public void NotifyAttachedApplicationsOfChange()
        {
            var itemsToRemove = new List<Guid>();
            foreach (var appGuid in AttachedApplicationsIds)
            {
                using (var desktopApp = new DesktopApplicationConnection(this, appGuid))
                {
                    try
                    {
                        desktopApp.WindowsServiceStatusChanged(IsWindowsServiceActive);
                    }
                    catch (Exception ex)
                    {
                        itemsToRemove.Add(appGuid);
                    }
                }
            }
            if (itemsToRemove.Count > 0)
            {
                //Loop through all application Guids and remove them from the active list
                foreach (var appGuid in itemsToRemove)
                {
                    AttachedApplicationsIds.Remove(appGuid);
                }
            }
        }
        #endregion

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
        #endregion
    }
}
